package com.ssm.core.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Set;

/**
 * 用户名密码验证
 *
 * @see UsernamePasswordAuthenticationFilter
 */
@Slf4j
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String uniqueId) {
        return new org.springframework.security.core.userdetails.User(
                "root", "root", true,
                true, true, true,
                obtainGrantedAuthorities());
    }

    /**
     * 获得登录者所有角色的权限集合.
     */
    private Set<GrantedAuthority> obtainGrantedAuthorities() {
        AuthorityUtils.commaSeparatedStringToAuthorityList("");
        return Collections.singleton(new SimpleGrantedAuthority("admin"));
    }
}
